<template>
  <div style="margin: 20px">
    <el-form :inline="true" :model="formInline" class="form-inline">
      <el-form-item label="工号">
        <el-input v-model="formInline.jobNumber" placeholder="请输入完整的工号" clearable/>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="formInline.userName" placeholder="根据姓名模糊查询" clearable/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.remindStatus">
          <el-option label="未处理" :value="100" />
          <el-option label="已处理" :value="200" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryUserBasicSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" v-loading="tableLoading" stripe style="width: 100%; height: 80%">
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="userName" label="姓名"/>
      <el-table-column prop="userIdNumber" label="身份证" :show-overflow-tooltip="true"/>
      <el-table-column prop="jobNumber" label="工号"/>
      <el-table-column prop="remindType" label="提醒类型">
        <template #default="scope">
          {{ tableRemindType(scope.row.remindType) }}
        </template>
      </el-table-column>
      <el-table-column prop="remindContent" label="提醒内容"/>
      <el-table-column prop="remindDatetime" label="提醒时间"/>
      <el-table-column prop="remindStatus" label="提醒状态">
        <template #default="scope">
          {{ tableRemindStatus(scope.row.remindStatus) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template #default="scope">
          <el-button
              size="small"
              type="warning"
              @click="handleAgreement(scope.$index, scope.row)"
          >
            查看协议
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleDeal(scope.$index, scope.row)"
          >
            处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          v-model:total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import {ref, onMounted, reactive} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus'
import UserBasic from '@/api/UserBasic.js';
import Remind from '@/api/Remind.js';
import { useRouter } from 'vue-router'

const router = useRouter()
const tableData = ref([]);
const tableLoading = ref(false)

const remindTypeMap = {
  1: '合同相关提醒'
}
const tableRemindType = (remindType) => {
  return remindTypeMap[remindType]
}

const remindStatusMap = {
  100: '未处理',
  200: '已处理'
}
const tableRemindStatus = (remindStatus) => {
  return remindStatusMap[remindStatus]
}

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const handleSizeChange = (val: number) => {
  formInline.pageSize = val;
  queryTableData(formInline)
}
const handleCurrentChange = (val: number) => {
  formInline.pageNo = val;
  queryTableData(formInline)
}

// 查询表单
const formInline = reactive({
  pageNo: currentPage.value,
  pageSize: pageSize.value,
  jobNumber: '',
  userName: '',
  remindStatus: 100,
})
const queryUserBasicSubmit = () => {
  queryTableData(formInline)
}

onMounted(() => {
  queryTableData(formInline)
});

const queryTableData = (data) => {
  tableLoading.value = true
  UserBasic.queryForEmploymentExamine(data)
      .then(res => {
        currentPage.value = res.data.pageNo
        pageSize.value = res.data.pageSize
        total.value = res.data.recordsTotal
        tableData.value = res.data.rows
        tableLoading.value = false
      })
      .catch(error => {
        tableLoading.value = false
      })
};

const handleAgreement = (index, row) => {
  const id = row.id;
  router.push({ name: 'agreementView', query: { userId:id } });
}

const handleDeal = (index, row) => {
  ElMessageBox.confirm(
      '状态将发生改变，是否继续？',
      'Warning',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        const body = ref({
          id: row.remindId,
        });
        Remind.dealRemind(body.value)
            .then(res => {
              ElMessage.success('已处理')
              queryTableData(formInline)
            })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消',
        })
      })
}

</script>

<style scoped>
.form-inline {
  margin: 20px;
}
.form-inline .el-input {
  --el-input-width: 200px;
}

.form-inline .el-select {
  --el-select-width: 200px;
}
</style>